package com.dbsop.domain.rest.res;

import com.dbsop.common.enums.DecideFlag;
import com.dbsop.common.enums.FlagConnect;
import com.dbsop.domain.AbsConditionQuery;
import com.dbsop.domain.agg.AggOperation;
import com.dbsop.domain.rest.query.RestConditionQuery;
import com.dbsop.domain.transport.AggTransportOperation;
import com.dbsop.domain.transport.query.ConditionQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户端 进行解析
 */
public class RestUtils {

    /**
     * 范围查询符号 判断
     * @param decideFlag
     * @param rangeQueryBuilder
     * @param value
     */
    public static void decideFlagHandler(DecideFlag decideFlag,
                                  RangeQueryBuilder rangeQueryBuilder,Object value){
        switch (decideFlag){
            case GT:
                rangeQueryBuilder.gt(value);
                break;
            case LT:
                rangeQueryBuilder.lt(value);
                break;
            case GTE:
                rangeQueryBuilder.gte(value);
                break;
            case LTE:
                rangeQueryBuilder.lte(value);
                break;
        }
    }

    /**
     * 连接符 前后链接
     * @param subConnect
     * @return
     */
    public static void flagConnectHandler(FlagConnect subConnect,BoolQueryBuilder boolQueryBuilder,
                                                      QueryBuilder ... builders){
        for (int i = 0; i < builders.length; i++) {
            switch (subConnect){
                case OR:
                    boolQueryBuilder.should(builders[i]);
                    break;
                case AND:
                    boolQueryBuilder.must(builders[i]);
                    break;
                case NOT:
                    boolQueryBuilder.mustNot(builders[i]);
                    break;
            }

        }
    }

    /**
     * 子查询过程
     * @param subQueries
     * @return
     */
    public static <T extends AbsConditionQuery> BoolQueryBuilder subBoolQueryBuilder(List<T> subQueries){
        if (subQueries == null)return null;
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        for (AbsConditionQuery query : subQueries) {
            RestConditionQuery subQuery = (RestConditionQuery) query;
            QueryBuilder queryBuilder = subQuery.paringBoolQuery();
            flagConnectHandler(subQuery.getFlagConnect(),boolQueryBuilder,queryBuilder);

            QueryBuilder subQueryBuilder = subQuery.getSubQueryBuilder();
            if (subQueryBuilder!=null){
                flagConnectHandler(subQuery.getSubConnect(),boolQueryBuilder,subQueryBuilder);
            }

        }
        return boolQueryBuilder;
    }


    /**
     * 聚合操作获取
     * @param aggOperations
     * @return
     */
    public static List<AggregationBuilder> paresAggregationBuilders(List<AggOperation> aggOperations){
        if (aggOperations == null || aggOperations.size() ==0)return null;
        List<AggregationBuilder> aggregationBuilders = new ArrayList<AggregationBuilder>();
        for (AggOperation aggOperation : aggOperations) {

            aggregationBuilders.add(((AggTransportOperation)aggOperation).aggregationBuilder());
        }
        return aggregationBuilders;
    }
}
